package org.pvt.gui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import org.jetbrains.annotations.NotNull;
import org.pvt.R;
import org.pvt.model.Model;

import static android.content.DialogInterface.OnClickListener;
import static org.pvt.model.db.DBUtils.getName;

/**
 * @author Pavel Talanov
 */
public final class EventAttrCRUDCursorAdapter extends CursorAdapter {

    @NotNull
    private final EventAttrCRUDActivity activity;

    public EventAttrCRUDCursorAdapter(@NotNull EventAttrCRUDActivity activity, @NotNull Cursor cursor) {
        super(activity, cursor);
        this.activity = activity;
    }

    @Override
    public View newView(final Context context, Cursor cursor, ViewGroup parent) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.item_with_buttons, parent, false);
        int idColumnIndex = cursor.getColumnIndex(Model.ID);
        long itemId = cursor.getLong(idColumnIndex);
        setUpView(cursor, view, itemId);
        return view;
    }

    private void setUpView(@NotNull Cursor cursor,
                           @NotNull View view,
                           long id) {
        setUpTextView(cursor, view);
        setUpEditButton(view, id);
        setUpDeleteButton(view, id);
    }

    private void setUpTextView(@NotNull Cursor cursor, @NotNull View view) {
        String name = getName(cursor);
        TextView name_text = (TextView) view.findViewById(R.id.itemText);
        if (name_text != null) {
            name_text.setText(name);
        }
    }

    private void setUpEditButton(@NotNull View view, final long id) {
        Button editButton = (Button) view.findViewById(R.id.editButton);
        editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LayoutInflater factory = LayoutInflater.from(activity);
                final View textEntryView = factory.inflate(R.layout.new_text_dialog, null);
                new AlertDialog.Builder(activity)
                        .setTitle(R.string.editDialogTitle)
                        .setView(textEntryView)
                        .setPositiveButton(android.R.string.ok, new OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                EditText textEdit = (EditText) textEntryView.findViewById(R.id.newNameEdit);
                                String newText = textEdit.getText().toString();
                                if (activity.validateText(newText)) {
                                    activity.updateItem(id, newText);
                                    Toast.makeText(activity, R.string.updateSuccessfulMessage, Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .setNegativeButton(android.R.string.cancel, null)
                        .show();

            }
        });
    }

    private void setUpDeleteButton(@NotNull View view, final long id) {
        Button deleteButton = (Button) view.findViewById(R.id.deleteButton);
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDeleteAlertDialog(id);
            }
        });
    }

    @NotNull
    private AlertDialog showDeleteAlertDialog(final long id) {
        return new AlertDialog.Builder(activity)
                .setTitle(R.string.deleteDialogTitle)
                .setMessage(R.string.deleteDialogMessage)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setPositiveButton(android.R.string.yes, new OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        activity.deleteItem(id);
                        Toast.makeText(activity, R.string.deleteSuccessfulMessage, Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton(android.R.string.no, null).show();
    }

    @Override
    public void bindView(View v, Context context, Cursor cursor) {
        //do nothing
    }
}
